package com.msg.impl;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import com.msg.ShowMessage;

/**
 * Title: <br>
 * Description:  <br>
 * Copyright: WangGuangya<br>
 * Company: WangGuangya<br>
 * @author WangGuangya
 * @version 1.0
 * @date 2018-02-23
 */

public class Log2File implements ShowMessage{

	File file;
	
	BufferedOutputStream bos;
	
	public Log2File(String filename){
		file = new File(filename);
		try {
			bos = new BufferedOutputStream(new FileOutputStream(file));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			bos = null;
		}
	}
	
	public Log2File(File file){
		this.file = file;
		try {
			bos = new BufferedOutputStream(new FileOutputStream(this.file));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			bos = null;
		}
	}
	
	@Override
	public void print(String str) {
		System.out.println(str);
		if (null != bos && null != str) {
			try {
				bos.write(str.getBytes());
				bos.flush();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	
	@Override
	public void close() {
		try {
			if(null != bos) {
				bos.flush();
				bos.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void append(String str) {
		print(str);
	}
}
